package com.web.controller.api;

import com.web.annotation.UserAccess;
import com.web.config.BaseReqConfig;
import com.web.config.KftPayCommConfig;
import com.web.dao.KftApplyMapper;
import com.web.dao.StoreMapper;
import com.web.domain.ApiResponse;
import com.web.dto.OnlineOrderDto;
import com.web.dto.RefundOrderDto;
import com.web.entity.KftApply;
import com.web.entity.Store;
import com.web.kftsdk.api.KftPayConfigStorage;
import com.web.kftsdk.api.KftPayService;
import com.web.kftsdk.common.bean.PayRes;
import com.web.kftsdk.entity.KppRefundReq;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import static java.math.BigDecimal.ROUND_HALF_DOWN;

/**快付通申请退款
 *
 * @author Welcome Chen
 * @date 2018-08-23
 */

@RestController
@RequestMapping("api")
public class KppRefundController {
@Autowired
private KftPayCommConfig kftPayCommConfig;
@Autowired
private BaseReqConfig baseReqConfig;
@Autowired
private StoreMapper storeMapper;
@Autowired
private KftApplyMapper kftApplyMapper;
    /**
     * 快付通申请退款
     * 退款分为全额退和部分退,部分退的话按照消费者和商家协商好的金额和原来的订单支付金额的比率
     * 来分别把订单支付时系统的2级商户和商户分账的金额乘以比率,然后把这笔钱分给退款,也就是退给消费者
     */
    @UserAccess
    @RequestMapping("/app/refund/kftApplyRefund")
    public ApiResponse allyRefund(RefundOrderDto refundOrderDto){
        ApiResponse res = new ApiResponse(  );
                //只允许退30天以内的交易
                    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                        //给定的时间
                    Date createOrder = refundOrderDto.getCreateTime();
                       //当前时间处理
                    Calendar cal = Calendar.getInstance();
                    cal.set( Calendar.HOUR_OF_DAY, 0);
                    cal.set(Calendar.MINUTE, 0);
                    cal.set(Calendar.SECOND, 0);
                    cal.set(Calendar.MILLISECOND, 0);
                    //给定时间处理
                    Calendar setCal = Calendar.getInstance();
                    setCal.setTime(createOrder);
                    setCal.set(Calendar.HOUR_OF_DAY, 0);
                    setCal.set(Calendar.MINUTE, 0);
                    setCal.set(Calendar.SECOND, 0);
                    setCal.set(Calendar.MILLISECOND, 0);
                    long dayDiff =(setCal.getTimeInMillis()-cal.getTimeInMillis())/(1000*60*60*24);
                     if(dayDiff <= 30L){
                          //交易在30天以内

                         KftPayConfigStorage kftPayConfigStorage = new KftPayConfigStorage();
                         kftPayConfigStorage.setPackPath(kftPayCommConfig.getPackPath());
                         kftPayConfigStorage.setSftpPath(kftPayCommConfig.getSftpPath());
                         kftPayConfigStorage.setSftpHost(kftPayCommConfig.getSftpHost());
                         kftPayConfigStorage.setSftpUsername(kftPayCommConfig.getSftpUsername());
                         kftPayConfigStorage.setSftpPassword(kftPayCommConfig.getSftpPassword());
                         kftPayConfigStorage.setKeyPrivate(kftPayCommConfig.getKeyPrivate());
                         kftPayConfigStorage.setDevGateWay(kftPayCommConfig.getDevGateWay());
                         kftPayConfigStorage.setGateWay(kftPayCommConfig.getGateWay());
                         kftPayConfigStorage.init();
                         KftPayService kftPayService = new KftPayService(kftPayConfigStorage);
                         KppRefundReq kppRefundReq =new KppRefundReq();
                         kppRefundReq.setService( "kpp_refund" );
                         kppRefundReq.setVersion( baseReqConfig.getVersion() );
                         kppRefundReq.setCharset( baseReqConfig.getCharset() );
                         kppRefundReq.setLanguage( baseReqConfig.getLanguage() );
                         kppRefundReq.setCallerIp( "60.186.219.109" );
                         kppRefundReq.setMerchantId( "2018072300096619" );

                         Store store = storeMapper.selectByPrimaryKey( refundOrderDto.getStoreId() );
                         //查询出二级商户id
                        KftApply kftApply  =  kftApplyMapper.findByMid( store.getmId() );
                         kppRefundReq.setSecMerchantId( kftApply.getKftId() );
                         String orderNum = "";
                         Date date = new Date(  );
                         SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
                         orderNum += sdf.format(date) + date.getTime();
                         kppRefundReq.setOrderNo( orderNum );
                         kppRefundReq.setOriginalOrderNo( refundOrderDto.getOrderNum() );
                         kppRefundReq.setTradeName( refundOrderDto.getTradeName() );
                         kppRefundReq.setTradeTime(refundOrderDto.getCreateTime().toString()  );
                         //判断是全额退款还是部分退款
                         if("all".equals( refundOrderDto.getAllRefund()) ){
                             //全额退款


                        }else{
                             //部分退款
                                //从全部分账部分取回用来退款(分账的2个二级商户分别取出相同的比率出来)
                                  //退款比例
                             BigDecimal refundRate = refundOrderDto.getRefundMoney().divide(refundOrderDto.getTotalPrice(), 2, ROUND_HALF_DOWN);
                             BigDecimal secMerchant = null;
                             if(secMerchant!=null){

                             }else {
                                 //从部分分账取回用来退款
                             }
                         }
                         kppRefundReq.setAmount( refundOrderDto.getRefundMoney().toString());
                         kppRefundReq.setCurrency( "CNY" );
                         kppRefundReq.setRemark( refundOrderDto.getRemark() );
                         kppRefundReq.setIsGuarantee( "" );
                         kppRefundReq.setIsSplit( "1" );
                         kppRefundReq.setSplitInfo( "" );

                         PayRes refundRes = kftPayService.kppRefund( kppRefundReq );

                     }

                return res;
    }



}
















